package org.wangchaocheng.simpleadmin.statemachine;

import com.alibaba.cola.statemachine.Action;
import com.alibaba.cola.statemachine.StateMachine;
import org.springframework.stereotype.Service;
import org.wangchaocheng.simpleadmin.statemachine.enums.EventEnum;
import org.wangchaocheng.simpleadmin.statemachine.enums.StateEnum;

@Service
public class ActionService {
    /**
     * 通过执行动作
     *
     * @return Action
     */
    public Action<StateEnum, EventEnum, StateMachineContext> downloadOriginalPackage() {

        return (from, to, event, context) -> {
            System.out.println("from:"+from+" to:"+to+" on:"+event+" context:" + context);
            StateMachine<StateEnum, EventEnum, StateMachineContext> stateMachine = context.getStateMachine();
            stateMachine.fireEvent(to, EventEnum.START_SIGNATURE, context);
        };
    }

    public Action<StateEnum, EventEnum, StateMachineContext> sign() {

        return (from, to, event, context) -> {
            System.out.println("from:"+from+" to:"+to+" on:"+event+" context:" + context);
            try {
                Thread.sleep(10000);
                System.out.println("sleep time");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            context.setResult(true);
        };
    }
}
